/**
 * Copyright (C) 2009 Matteo Merli <mm@merlimat.org>
 * 
 * $Id: Scope.java 14 2009-01-04 18:01:41Z matteo.merli $
 * $URL: http://pxl-lang.googlecode.com/svn/trunk/src/main/java/pxl/Scope.java $
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pxl;

import java.util.Map;
import java.util.Set;

import pxl.types.PxlObject;

import com.google.common.collect.Maps;

/**
 * @author Matteo Merli
 */
public class Scope
{

    private final String name;
    
    private final Scope parentScope;

    private final Map<String, PxlObject> symbolTable = Maps
            .newConcurrentHashMap();

    public Scope( String name, Scope parentScope )
    {
        this.name = name;
        this.parentScope = parentScope;
    }

    public void put( String name, PxlObject obj )
    {
        symbolTable.put( name, obj );
    }

    public PxlObject get( String name )
    {
        return symbolTable.get( name );
    }

    public String getName()
    {
        return name;
    }
    
    public Scope getParent()
    {
        return parentScope;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append( "Scope " ).append( name ).append( ": { " );
        boolean first = true;

        for ( String key : symbolTable.keySet() )
        {
            if ( !first )
                sb.append( ", " );
            else
                first = false;
            sb.append( '\'' ).append( key ).append( '\'' ).append( ':' );
            sb.append( symbolTable.get( key ).__repr__().toString() );
        }
        sb.append( " }" );
        return sb.toString();
    }

    public Set<String> getSymbols()
    {
        return symbolTable.keySet();
    }

}
